/**
 * Class: ParticleSource
 *
 * Purpose: 
 *
 * Created: Jan 23, 2010 @ 11:56:37 PM
 * @author Ken Scott
 */
package org.openfantasia.ps.particle;

import org.openfantasia.ps.actor.Actor;
import org.openfantasia.ui.PFrame;

import java.util.LinkedList;

public class DefaultParticleFactory implements ParticleFactory {

	protected PFrame frame;

	protected Actor childActor;

	protected LinkedList<Particle> pool = new LinkedList<Particle>();


	public DefaultParticleFactory(PFrame frame, Actor childActor) {
		this.frame = frame;
		this.childActor = childActor;
	}


	public Particle createParticle() {
		return new Particle();
	}


	public Particle newParticle() {
		Particle result = null;

		synchronized (pool) {
			if (!pool.isEmpty()) {
				result = take();
			}
			else {
				result = createParticle();
			}
		}

		result.setFactory(this);
		result.setActor(childActor);

		return result;
	}


	public void put(Particle particle) {
		synchronized (pool) {
			pool.addLast(particle);
		}
	}


	public Particle take() {
		synchronized (pool) {
			Particle result = null;
			if (!pool.isEmpty()) {
				result = pool.removeFirst();
				result.clear();
			}
			return result;
		}
	}
}